$(document).ready(function(){
    var page=0
    getAll(page)
    $("#but1").click(function () {
        if(page!=0){
            page=page-1
            getAll(page)
        }
    })
    $("#but2").click(function () {
        page=page+1
        getAll(page)
    })

    function getAll(page){
        $.get("/artmanage/findall",{page:page}, function (data) {
            console.log(data)
            if(data.news.totalElements>5){
            if(data.news.number==0){
                $("#but1").hide();
                $("#but2").show();
            }
            if((data.news.number-data.news.totalPages+1)==0){
                $("#but2").hide();
                $("#but1").show();

            }
            }else {
                $("#but1").hide();
                $("#but2").hide();
            }
            var html=''
            for (i=0;i<data.news.content.length;i++){
                var date=timeChange(data.news.content[i].date)
              html=html+ ('<tr>\n' +
                    '                <a href="/artmanage/creatNew.html">   <td>'+data.news.content[i].id+'</td>\n' +
                    '                    <td>'+data.news.content[i].title+'</td>\n' +
                    '                    <td>'+date+'</td>\n' +
                    '                    <td><a href="news.html?'+data.news.content[i].id+'">'+data.news.content[i].text+'</a></td>\n' +
                    '                    <td>\n' +
                    '                        <div class="btn-group" >\n' +
                    '                               <button type="button" class="btn btn-primary" onclick="goto('+data.news.content[i].id+')" > 修改</button></a>\n' +
                    '                                <button type="button" class="btn btn-primary" onclick="delet('+data.news.content[i].id+')"> 删除 </button>\n' +
                    '                        </div>\n' +
                    '                    </td>\n' +
                    '                </tr>')
            }
            $("#tbody").html(html)
        })
    }
    $("#search").click(function () {
       var key= $("#key").val()
        $.get("/artmanage/search",{key:key},function (data) {
            $("#tbody").html('')
            for (i=0;i<data.length;i++){
                var date=timeChange(data[i].date)
                $("#tbody").append('<tr>\n' +
                    '                <a href="/artmanage/creatNew.html">   <td>'+data[i].id+'</td>\n' +
                    '                    <td>'+data[i].title+'</td>\n' +
                    '                    <td>'+date+'</td>\n' +
                    '                    <td><a href="news.html?'+data[i].id+'">'+data[i].text+'</a></td>\n' +
                    '                    <td>\n' +
                    '                        <div class="btn-group" >\n' +
                    '                               <button type="button" class="btn btn-primary" onclick="goto('+data[i].id+')" > 修改</button></a>\n' +
                    '                                <button type="button" class="btn btn-primary" onclick="delet('+data[i].id+')"> 删除 </button>\n' +
                    '                        </div>\n' +
                    '                    </td>\n' +
                    '                </tr>')
            }
        })
    })
    function timeChange(date1){
        var date=new Date(date1);
        var cy=date.getFullYear()+'年';
        var cm=date.getMonth()+1+'月';
        var cd=date.getDate()+'日';
        var cdate=cy+cm+cd;
        return cdate;
    }


});
function delet(id) {
    if(confirm("确认删除？")){
        $.post("/artmanage/delete",{id:id},function (data) {
            if(data){
                alert("删除成功"+id)
                window.location.href="index.html"
            }else {
                alert("出现错误")
            }
        })

    }
}
function goto(id) {
    window.location.href="updateNews.html?"+id;
}